package com.jinyu.jyly.ui.mine.setting;

import android.app.Activity;

import com.jinyu.jyly.base.BasePresenter;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.net.DataManager;

import java.util.List;

import okhttp3.MultipartBody;
import rx.Subscription;
import rx.functions.Action1;

public class SettingPresenter extends BasePresenter<SettingContract.View> implements SettingContract.Presenter {

    public SettingPresenter(Activity activity, SettingContract.View view) {
        super(activity, view);

    }

    /**
     * 修改昵称
     */
    @Override
    public void setNickName(String param) {
        Subscription subscribe = DataManager.getInstance().setNickName(param)
                .subscribe(new Action1<AllInfoBean>() {
                    @Override
                    public void call(AllInfoBean InfoData) {
                        if (InfoData != null) {
                            mView.setNickName(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }



    @Override
    public void updataImg(List<MultipartBody.Part> body) {
        Subscription subscribe = DataManager.getInstance().updataImg(body)
                .subscribe(new Action1<AllInfoBean>() {
                    @Override
                    public void call(AllInfoBean InfoData) {
                        if (InfoData != null) {
                            mView.onUpdataImg(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 检查是否需要更新app
     * @param param
     */
    @Override
    public void getUpdataApp(String param) {
        Subscription subscribe = DataManager.getInstance().getUpdataApp(param)
                .subscribe(new Action1<AllInfoBean>() {
                    @Override
                    public void call(AllInfoBean InfoData) {
                        if (InfoData != null) {
                            mView.onUpdatApp(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }


}
